package 力扣._392判断子序列;

public class Solution {
    public boolean isSubsequence(String s, String t) {
        //找到s,t的最大公共子序列，看它的长度是否与s相同
        //dp[i][j]: 以s[i-1],t[j-1]为尾的最长公共子序列的长度为dp[i][j]
        int[][] dp = new int[s.length() + 1][t.length() + 1];
        for (int i = 1; i <= s.length(); i++) {
            for (int j = 1; j <= t.length(); j++) {
                if (s.charAt(i - 1) == t.charAt(j - 1))
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                else
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
        return s.length() == dp[s.length()][t.length()];
    }
}
